package com.fe.class03;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @Description 用队列结构实现栈结构
 * @Author sosuke :-)
 * @Date 2021/12/12 12:17
 */
public class Code09_TwoQueueToStack {

    public static class TwoQueueToStack<T> {
        private Queue<T> queue; //用于新增数据和弹出数据
        private Queue<T> help; // 用于弹出数据的中转站

        public TwoQueueToStack() {
            queue = new LinkedList<>();
            help = new LinkedList<>();
        }

        public void push(T data) {
            queue.offer(data);
        }

        public T pop() {
            // 倒数据，最后剩一个
            while (queue.size() > 1) {
                help.offer(queue.poll());
            }
            T ans = queue.poll();
            // 交换引用
            Queue<T> temp = queue;
            queue = help;
            help =  temp;
            return ans;
        }

        public T peek() {
            // 倒数据，最后剩一个
            while (queue.size() > 1) {
                help.offer(queue.poll());
            }
            T ans = queue.poll();
            help.offer(ans); // 放入help中
            // 交换引用
            Queue<T> temp = queue;
            queue = help;
            help =  temp;
            return ans;
        }

        public boolean isEmpty() {
            return queue.isEmpty();
        }
    }

    public static void main(String[] args) {
        int testTimes = 10000;
        int maxValue = 1000;
        TwoQueueToStack<Integer> twoQueueToStack = new TwoQueueToStack<>();
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < testTimes; i++) {
            if (twoQueueToStack.isEmpty()) {
                if (!stack.isEmpty()) {
                    System.out.println("TwoQueueToStack oops (isEmpty)");
                    return;
                }
                int num = (int) (Math.random() * (maxValue + 1));
                twoQueueToStack.push(num);
                stack.push(num);
            } else {
                double random = Math.random();
                if (random < 0.25) {
                    int num = (int) (Math.random() * (maxValue + 1));
                    twoQueueToStack.push(num);
                    stack.push(num);
                } else if (random < 0.5) {
                    if (!twoQueueToStack.peek().equals(stack.peek())) {
                        System.out.println("TwoQueueToStack oops (peek)");
                        return;
                    }
                } else if (random < 0.75) {
                    if (!twoQueueToStack.pop().equals(stack.pop())) {
                        System.out.println("TwoQueueToStack oops (pop)");
                        return;
                    }
                } else {
                    if (twoQueueToStack.isEmpty() != stack.isEmpty()) {
                        System.out.println("TwoQueueToStack oops (isEmpty)");
                        return;
                    }
                }
            }
        }
        System.out.println("Nice");
    }

}
